//
//  HomeFlowStrictElectionStoreCell.m
//  BaseProject
//
//  Created by 王洋 on 2020/7/13.
//  Copyright © 2020 王洋. All rights reserved.
//

#import "StrictStoreBottomCell.h"

#import "StrictStoreMixCell.h"


#import "HomeFlowStrictElectionStoreCell.h"

@interface HomeFlowStrictElectionStoreCell ()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>{
    
}



@end

@implementation HomeFlowStrictElectionStoreCell

+ (CGFloat)calculateHeight {
    
    CGFloat collectionHeight =  ((TTScreenWidth - 20)/3)*1.54 + 5;
    return collectionHeight;
}

- (void)awakeFromNib {
    [super awakeFromNib];

    [self initView];
}

- (void)initView {
    
    CGFloat width = TTScreenWidth - 20;
    CGFloat cellWidth;
    cellWidth = width/3 - 4;
    UICollectionViewFlowLayout *flowLaout1 = [[UICollectionViewFlowLayout alloc]init];
    flowLaout1.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    flowLaout1.minimumInteritemSpacing = 5;
    flowLaout1.minimumLineSpacing = 5;
    flowLaout1.itemSize = CGSizeMake(cellWidth, cellWidth*1.155);

    self.collectionview.collectionViewLayout = flowLaout1;
    self.collectionview.tag = 100;
    self.collectionview.delegate = self;
    self.collectionview.dataSource = self;
    self.collectionview.showsHorizontalScrollIndicator = NO;
    self.collectionview.showsVerticalScrollIndicator = NO;
    [self.collectionview registerNib:[UINib nibWithNibName:@"StrictStoreMixCell" bundle:nil] forCellWithReuseIdentifier:@"StrictStoreMixCellID"];

    
    UICollectionViewFlowLayout *flowLaout2 = [[UICollectionViewFlowLayout alloc]init];
    flowLaout2.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    flowLaout2.minimumInteritemSpacing = 5;
    flowLaout2.minimumLineSpacing = 5;
    flowLaout2.itemSize = CGSizeMake(cellWidth, cellWidth*0.386);
    self.bottomCollectionview.collectionViewLayout = flowLaout2;
    self.bottomCollectionview.tag = 200;
    self.bottomCollectionview.delegate = self;
    self.bottomCollectionview.dataSource = self;
    self.bottomCollectionview.showsHorizontalScrollIndicator = NO;
    self.bottomCollectionview.showsVerticalScrollIndicator = NO;
    [self.bottomCollectionview registerNib:[UINib nibWithNibName:@"StrictStoreBottomCell" bundle:nil] forCellWithReuseIdentifier:@"StrictStoreBottomCellID"];
    
}


- (void)fillCellWithModel:(HomeADItemModel *)model bottomDataArray:(NSMutableArray <HomeADItemEventModel *>*)bottomArray {

    self.topHeight.constant = model.itemHeight - 50;
    
    self.dataArray = @[].mutableCopy;
    self.bottomDataArray = @[].mutableCopy;
    
    self.dataArray = model.sysAdvertisement.sysAdvEventList;
    self.bottomDataArray = bottomArray.mutableCopy;
    
    [self.collectionview reloadData];
    [self.bottomCollectionview reloadData];

}

- (void)layoutSubviews {
    [super layoutSubviews];
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    

    switch (collectionView.tag) {
        case 100:{

            StrictStoreMixCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"StrictStoreMixCellID" forIndexPath:indexPath];
            [cell.topImageView sd_setImageWithURL:[NSURL URLWithString:self.dataArray[indexPath.row].url] placeholderImage:[UIImage imageNamed:@"placeholder"]];
            
            return cell;
        } break;
        case 200:{

            StrictStoreBottomCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"StrictStoreBottomCellID" forIndexPath:indexPath];
            [cell.mainImageView sd_setImageWithURL:[NSURL URLWithString:self.bottomDataArray[indexPath.row].url] placeholderImage:[UIImage imageNamed:@"placeholder"]];
            cell.mainTitleLab.text = self.bottomDataArray[indexPath.row].sellerName;
            return cell;
        } break;
        default:
            break;
    }
    
    
    StrictStoreMixCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"StrictStoreMixCellID" forIndexPath:indexPath];
    return cell;
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    switch (collectionView.tag) {
        case 100:{

            return self.dataArray.count;
        } break;
        case 200:{

            return self.bottomDataArray.count;
        } break;
        default:
            break;
    }
    return 0;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    [[StatisticManager shareManager]insertBrowseUserActiveQuantityWithType:@"7"];
    switch (collectionView.tag) {
        case 100:{
            if (self.HomeFlowStrictElectionCellBlock) {
                self.HomeFlowStrictElectionCellBlock(HomeFlowStrictElectionType_top ,indexPath.row);
            }
            
        } break;
        case 200:{
            if (self.HomeFlowStrictElectionCellBlock) {
                self.HomeFlowStrictElectionCellBlock(HomeFlowStrictElectionType_bottom ,indexPath.row);
            }
        } break;
        default:
            break;
    }
    
}

@end
